import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

Vue.use(VueRouter)
const routes = [{
        path: '/',
        name: '/',
        component: () =>
            import ('@/layout/index'),
        redirect: '/home',
        children: [{
                path: 'home',
                name: 'home',
                component: () =>
                    import ('@/layout/Home'),
            },
            {
                path: 'users',
                name: 'users',
                component: () =>
                    import ('@/views/users'),
            },
            {
                path: 'goods',
                name: '/',
                component: () =>
                    import ('@/views/Commodity/goods/index'),
                redirect: '/goods/index',
                children: [{
                    path: 'index',
                    name: 'goods',
                    component: () =>
                        import ('@/views/Commodity/goods/GoodsList'),
                }, {
                    path: 'addGoods',
                    name: 'addGoods',
                    component: () =>
                        import ('@/views/Commodity/components/AddGoods'),
                }]
            }, {
                path: 'params',
                name: 'params',
                component: () =>
                    import ('@/views/Commodity/params'),
            }, {
                path: 'categories',
                name: 'categories',
                component: () =>
                    import ('@/views/Commodity/categories'),
            },
            {
                path: 'orders',
                name: 'orders',
                component: () =>
                    import ('@/views/Order'),
            },
            {
                path: 'rights',
                name: 'rights',
                component: () =>
                    import ('@/views/Authority/rights'),
            },
            {
                path: 'roles',
                name: 'roles',
                component: () =>
                    import ('@/views/Authority/roles'),
            }, {
                path: 'reports',
                name: 'reports',
                component: () =>
                    import ('@/views/Reports'),
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () =>
            import ('@/views/login/login'),
        children: []
    }
]
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
    // push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
        if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
        return originalPush.call(this, location).catch(err => err)
    }
    // replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
    return originalReplace.call(this, location).catch(err => err)
}


const router = new VueRouter({
    // mode: 'history',
    base: process.env.BASE_URL,
    routes
})

//在页面跳转使用
router.beforeEach((to, from, next) => {
    //开始进度条
    NProgress.start();
    // 继续路由
    next();
});
router.afterEach(transition => {
    //结束进度条
    NProgress.done();
});

NProgress.configure({
    easing: 'ease', // 动画方式
    speed: 500, // 递增进度条的速度
    showSpinner: true, // 是否显示加载
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3 // 初始化时的最小百分比
})
export default router